import { _decorator, Component, Node, ProgressBar } from 'cc';
import { SoundMgr } from '../Managers/SoundMgr';
import { SpriteUtil } from '../Utils/SpriteUtil';
import { DB } from '../Data/DB';
import { PanelUtility } from '../Utility/PanelUtility';
import { ResMgr } from '../Managers/ResMgr';
import { Prefab_Type, RES_TYPE } from '../Data/GameConstant';
import ResPkg from './GameResPkg';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {
    //
    public static Instance:Game = null;

    onLoad():void{
        if(null === Game.Instance){
            Game.Instance = this;
        }else{
            this.destroy();
            return;
        }
    }

    public StartGame():void{
        var resLoading:Node = this.node.getChildByName("ResLoading");
        var progressBar:ProgressBar = resLoading.getChildByName("ProgressBar").getComponent(ProgressBar);
        //预加载资源
        ResMgr.Instance.preloadResPkg(ResPkg, (now:number, total:number)=>{
            progressBar.progress = now / total;
        }, ()=>{
            this.EnterGameScene();
            progressBar.node.active = false;// resLoading.destroy();
        });
        //end
    }

    public EnterGameScene():void{
        SoundMgr.Instance.playBgMusic();
        SpriteUtil.Instance.preload();
        DB.getInstnace().prepare();
        //
        console.log("[Game] EnterGameScene")
        PanelUtility.Instance.showPopLayout(RES_TYPE.GUI, Prefab_Type.ChessUI);
    }
}

